package com.kdd.campus.sh.controller;

import com.kdd.campus.common.entity.CommonResult;
import com.kdd.campus.common.util.RedisUtil;
import com.kdd.campus.sh.dto.CommentDto;
import com.kdd.campus.sh.dto.ProductInfo;
import com.kdd.campus.sh.po.ShGallery;
import com.kdd.campus.sh.po.ShProduct;
import com.kdd.campus.sh.po.User;
import com.kdd.campus.sh.service.*;
import com.kdd.campus.sh.util.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "商品管理")
@RestController
@RequestMapping("/shProduct")
@CrossOrigin
@Validated
public class ShProductController extends BaseController {

    @Autowired
    private AdService adService;

    @Autowired
    private ShProductService productService;

    @Autowired
    private UserService userService;

    @Autowired
    private ShFavoriteService shFavoriteService;

    @Autowired
    private ESManagerService esManagerService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private GalleryService galleryService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Value("${product.pageSize}")
    private int pageSize;

    /***
     * 分页查询商品展示信息
     * @param page:当前页
     * @return
     */
    @ApiOperation(value = "分页查询商品展示信息(首页信息展示)",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page", value = "当前页", dataType = "Integer"),
    })
    @GetMapping(value = "/index" )
    public CommonResult findPage(@NotNull Integer page){
        //调用ShProductService实现分页查询ShProduct
        List<ProductInfo> productInfoList = productService.findProductInfoPage(page, pageSize);
        return CommonResult.success(productInfoList);
    }

    @ApiOperation(value = "获取精品商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page", value = "当前页", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query",name = "searchMap", value = "排序的条件", dataType = "map"),
    })
    @GetMapping(value = "/quality" )
    public CommonResult getQualityProduct(@NotNull Integer page){
        List<ProductInfo> productInfoList = productService.getQualityProducts(page, pageSize);
        return CommonResult.success(productInfoList);
    }

    // 获取新品首发商品
    @ApiOperation(value = "获取新品首发商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "conditionMap", value = "排序的条件,如order=desc", dataType = "conditionMap"),
    })
    @GetMapping(value = "/new" )
    public CommonResult getNewProduct(@RequestParam Map<String,String> conditionMap) {
        // conditionMap含有排序规则 order=desc | order=asc
        List<ProductInfo> productInfoList = productService.getNewProduct(conditionMap);
        return CommonResult.success(productInfoList);
    }

    // 获取人气热门商品
    @ApiOperation(value = "获取人气热门商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "conditionMap", value = "排序的条件,如order=desc", dataType = "conditionMap"),
    })
    @GetMapping(value = "/hot" )
    public CommonResult getHotProduct(@RequestParam Map<String,String> conditionMap) {
        // conditionMap含有排序规则 order=desc | order=asc
        List<ProductInfo> productInfoList = productService.getHotProduct(conditionMap);
        return CommonResult.success(productInfoList);
    }

    /***
     * 根据分类查询商品
     * @param cid
     * @param page
     * @return
     */
    @ApiOperation(value = "根据二级分类查询商品",notes = "根据二级分类查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "cid", value = "分类的id", dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "page", value = "页码", dataType = "Integer"),
    })
    @GetMapping(value = "/category2/{cid}/{page}" )
    public CommonResult findByCategory2(@PathVariable Integer cid , @PathVariable int page){
        //调用ShProductService实现分页条件查询ShProduct
        ArrayList<ProductInfo> productInfos = productService.findByCategory2(cid, page, pageSize);
        return CommonResult.success(productInfos);
    }

    @ApiOperation(value = "根据一级分类查询商品",notes = "根据一级分类查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "cid", value = "分类的id", dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "page", value = "页码", dataType = "Integer"),
    })
    @GetMapping(value = "/category1/{cid}/{page}" )
    public CommonResult findByCategory1(@PathVariable Integer cid , @PathVariable int page){
        //调用ShProductService实现分页条件查询ShProduct
        ArrayList<ProductInfo> productInfos = productService.findByCategory1(cid, page, pageSize);
        return CommonResult.success(productInfos);
    }

    /***
     * 根据ID查询商品详细信息
     * @param productId
     * @return
     */
    @ApiOperation(value = "查询商品详情",notes = "根据ID查询ShProduct方法详情")
    @ApiImplicitParam(paramType = "path", name = "productId", value = "productId", dataType = "long")
    @GetMapping("/{productId}")
    public CommonResult findById(@PathVariable Long productId){
        Long userId = null;
        try {
            userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        } catch (Exception e) {}


        //调用ShProductService实现根据主键查询ShProduct
        // 查询商品信息
        ShProduct product = productService.findById(productId);

        // 同类商品
        ArrayList<ProductInfo> productList = productService.findByCategory2(product.getCategoryId(), 1, pageSize);

        // 获取商品的画廊
        List<ShGallery> galleryList = galleryService.findByProductId(productId);

        // 获取用户信息
        User user = userService.findById(product.getUserId());

        // 获取该商品的留言信息
        List<CommentDto> commentList = commentService.findByProductId(productId, userId);

        // 收藏信息
//        boolean isFavorite = shFavoriteService.isFavorite(user.getId() , shProduct.getId());

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("userInfo", user);
        resultMap.put("productInfo",product);
        resultMap.put("productList",productList);
//        resultMap.put("isFavorite",isFavorite);
        resultMap.put("comment", commentList);
        resultMap.put("gallery", galleryList);

        return CommonResult.success(resultMap);
    }
    @ApiOperation(value = "增加商品",notes = "增加商品")
    @ApiImplicitParam(paramType = "path", name = "product", value = "product", dataType = "ShProduct")
    @PostMapping
    public CommonResult saveProduct(@PathVariable ShProduct product){
       productService.add(product);
        return CommonResult.success();
    }


    @ApiOperation(value = "删除商品",notes = "删除商品")
    @ApiImplicitParam(paramType = "path", name = "productId", value = "productId", dataType = "long")
    @GetMapping
    public CommonResult deleteProduct(@PathVariable Long productId){
        productService.delete(productId);
        return CommonResult.success();
    }


    @ApiOperation(value = "修改商品",notes = "修改商品")
    @ApiImplicitParam(paramType = "path", name = "product", value = "product", dataType = "ShProduct")
    @PostMapping
    public CommonResult updateProduct(@PathVariable ShProduct product){
        productService.update(product);
        return CommonResult.success();
    }




    /**
     * 将数据导入ES数据库中
     */
    @GetMapping("import")
    public void importData(){
        esManagerService.importProductData();

    }


}
